package com.whfc.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.base.dao.AppDocumentManageDao;
import com.whfc.base.dto.AppDocumentManageDTO;
import com.whfc.base.entity.AppDocumentManage;
import com.whfc.base.service.AppDocumentManageService;
import com.whfc.common.result.PageVO;
import com.whfc.common.result.PageVOUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * (AppDocumentManage)表服务实现类
 *
 * @author makejava
 * @since 2022-07-13 15:31:14
 */
@DubboService(interfaceClass = AppDocumentManageService.class, version = "1.0.0")

public class AppDocumentManageServiceImpl implements AppDocumentManageService {
    @Autowired
    private AppDocumentManageDao appDocumentManageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AppDocumentManage queryById(Integer id) {
        return this.appDocumentManageDao.queryById(id);
    }

    @Override
    public List<AppDocumentManageDTO> queryByDocType(Integer deptId,Integer docType) {
        List<AppDocumentManageDTO> appDocumentManages = appDocumentManageDao.queryByDocType(deptId,docType,-1,null);
        if(appDocumentManages.isEmpty()){
            //初始化
            AppDocumentManage appDocumentManage = new AppDocumentManage();
            appDocumentManage.setDeptId(deptId);
            appDocumentManage.setDocType(0);
            appDocumentManage.setParentId(-1);
            appDocumentManage.setName("全部文件");
            appDocumentManageDao.insertSelective(appDocumentManage);
            appDocumentManages = appDocumentManageDao.queryByDocType(deptId,docType,-1,null);
        }
        appDocumentManages.forEach(appDocumentManageDTO -> appDocumentManageDTO.setChildFolder(getChildFolder(deptId,docType,appDocumentManageDTO.getId())));
        return appDocumentManages;
    }
    @Override
    public List<AppDocumentManageDTO> queryByDocType(Integer deptId,Integer docType,Integer folderId) {

        List<AppDocumentManageDTO> appDocumentManages = appDocumentManageDao.queryByDocType(deptId,docType,folderId,null);

        appDocumentManages.forEach(appDocumentManageDTO -> appDocumentManageDTO.setChildFolder(getChildFolder(deptId,docType,appDocumentManageDTO.getId())));
        return appDocumentManages;
    }
    List<AppDocumentManageDTO>getChildFolder(Integer deptId, Integer docType ,Integer folderId){
        List<AppDocumentManageDTO> appDocumentManages = appDocumentManageDao.queryByDocType(deptId, docType, folderId,null);
        appDocumentManages.forEach(appDocumentManage -> appDocumentManage.setChildFolder(getChildFolder(deptId, docType, appDocumentManage.getId())));
        return appDocumentManages;
    }

    @Override
    public PageVO<AppDocumentManageDTO> queryPageByDocType(Integer deptId, Integer docType, Integer folderId, Integer pageNum, Integer pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<AppDocumentManageDTO> appDocumentManages = appDocumentManageDao.queryByDocType(deptId,docType,folderId,name);
        return PageVOUtil.page(PageInfo.of(appDocumentManages));
    }

    @Override
    public PageVO<AppDocumentManageDTO> queryPageByDocType(Integer deptId, Integer docType, List<Integer> folderIds, Integer pageNum, Integer pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<AppDocumentManageDTO> appDocumentManages = appDocumentManageDao.queryByDocTypeAndFolderIds(deptId,docType,folderIds,name);
        return PageVOUtil.page(PageInfo.of(appDocumentManages));
    }

    /**
     * 分页查询
     *
     * @param appDocumentManage 筛选条件
     * @param pageRequest       分页对象
     * @return 查询结果
     */
//    @Override
//    public Page<AppDocumentManage> queryByPage(AppDocumentManage appDocumentManage, PageRequest pageRequest) {
//        long total = this.appDocumentManageDao.count(appDocumentManage);
//        return new PageImpl<>(this.appDocumentManageDao.queryAllByLimit(appDocumentManage, pageRequest), pageRequest, total);
//    }

    /**
     * 新增数据
     *
     * @param appDocumentManage 实例对象
     * @return 实例对象
     */
    @Override
    public AppDocumentManage insert(AppDocumentManage appDocumentManage) {
        this.appDocumentManageDao.insertSelective(appDocumentManage);
        return appDocumentManage;
    }

    /**
     * 修改数据
     *
     * @param appDocumentManage 实例对象
     * @return 实例对象
     */
    @Override
    public int update(AppDocumentManage appDocumentManage) {
        int update = this.appDocumentManageDao.update(appDocumentManage);
        return update;
    }


}
